package com.taojiazheng.serviceImpl.order;

import com.taojiazheng.common.ApplicationException;
import com.taojiazheng.dao.order.OrderDao;
import com.taojiazheng.dao.user.UserDao;
import com.taojiazheng.entity.Order;
import com.taojiazheng.entity.User;
import com.taojiazheng.mapper.JobinfoMapper;
import com.taojiazheng.mapper.OrderMapper;
import com.taojiazheng.service.order.OrderService;
import com.taojiazheng.so.order.OrderSo;
import com.taojiazheng.vo.Order.OrderVo;
import com.taojiazheng.vo.job.JobInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 赵成林
 * @date 2017/11/25 20:46
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private JobinfoMapper jobinfoMapper;

    @Autowired
    private OrderMapper orderMapper;

    /**
     * 创建订单之前
     * 判断是否可以创建新的订单
     *
     * @param orderSo
     * @return
     */
    public int createOrder(OrderSo orderSo, String token) {
        //return 0;
        if (!this.isCanCreate(orderSo, token)) {
            throw new ApplicationException(207, "有相同订单暂时不能创建");
        }
        Order order = new Order();
        order.setUserId(this.getUserId(token));
        order.setJobId(orderSo.getJobId());
        order.setDate(this.formatTime(new Date()));
        order.setUpdateDate(this.formatTime(new Date()));
        order.setStatus(0);
        order.setIsDelete(0);
        return orderDao.createOrder(order);

    }

    public List<OrderVo> getOrder(OrderSo orderSo, String token) {
        //return null;
        Order order = new Order();
        order.setJobId(orderSo.getJobId());
        order.setStatus(orderSo.getStatus());
        order.setUserId(this.getUserId(token));
        order.setId(orderSo.getId());
        return orderDao.getOrder(order);
    }

    protected int getUserId(String token) {
        User user = userDao.getUserByToken(token);
        return user.getId();
    }

    /**
     * 判断是不是可以创建该订单
     * @param orderSo
     * @param token
     * @return
     */
    protected boolean isCanCreate(OrderSo orderSo, String token) {
        orderSo.setStatus(0);
        List<OrderVo> list = this.getOrder(orderSo, token);
        if (list != null && list.size() > 0) {
            return false;
        }
        return true;
    }

    /**
     * 时间格式修改
     *
     * @param date
     * @return
     */
    protected String formatTime(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
        String dateTime = simpleDateFormat.format(date);
        return dateTime;
    }

    /**
     * 订单支付
     * 更新订单状态
     * 改变工作详情
     */
    public int updateOrder(OrderSo orderSo,String token) {
        User user = userDao.getUserByToken(token);
        if(!(user.getPassword().equals(orderSo.getPassword()))){
            throw new ApplicationException(212,"支付密码错误");
        }
        Order getOrder = orderMapper.selectByPrimaryKey(orderSo.getId());
        this.changNeedNum(getOrder.getJobId());
        Order order = new Order();
        order.setId(orderSo.getId());
        order.setStatus(1);
        order.setUpdateDate(this.formatTime(new Date()));
        return orderDao.updateOrder(order);
    }

    /**
     * 更新jobinfo
     * 修改需要的人数
     */
    protected int changNeedNum(Integer jobId) {
        synchronized (this) {
            JobInfoVo jobInfoVo = jobinfoMapper.getJobInfo(jobId);
            if (jobInfoVo.getNeedNum() < 0 || jobInfoVo.getNeedNum() == 0) {
                throw new ApplicationException(208, "该需求已经结束");
            }
            Map map = new HashMap();
            map.put("needNum", jobInfoVo.getNeedNum() - 1);
            map.put("jobId", jobId);
            return jobinfoMapper.updateNeedNum(map);
        }
        //return 0;

    }
    public int updateOrderFinished (OrderSo orderSo){
        Order order = new Order();
        order.setId(orderSo.getId());
        order.setStatus(2);
        order.setUpdateDate(this.formatTime(new Date()));
        return orderDao.updateOrder(order);
    }

    @Override
    public void deleteOrder(int id) {
        orderMapper.deleteOrder(id);
    }
}
